// #include "../common/net.hpp"
// #include "../common/dispatcher.hpp"
// #include "../client/rpccaller.hpp"
// #include <chrono>
// #include <iostream>
// #include <thread>

/* verson1-------test------*/

// void rpcResponseCallBack(const qiangsir::BaseConnection::ptr &conn, qiangsir::RpcResponse::ptr &msg)
// {
//     std::cout<<"收到rpc应答\n";
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
// }

// void topicResponseCallBack(const qiangsir::BaseConnection::ptr &conn,  qiangsir::TopicResponse::ptr &msg)
// {
//     std::cout<<"收到topic应答\n";
//     std::string body = msg->serialize();
//     std::cout << body << std::endl;
// }
// int main()
// {
//     qiangsir::Dispatcher::ptr dispatcher = std::make_shared<qiangsir::Dispatcher>();

//     dispatcher->registerHandler<qiangsir::RpcResponse>(qiangsir::MType::RSP_RPC,rpcResponseCallBack);
//     dispatcher->registerHandler<qiangsir::TopicResponse>(qiangsir::MType::RSP_TOPIC,topicResponseCallBack);

//     auto message_cb = std::bind(&qiangsir::Dispatcher::onMessage,dispatcher.get(),
//     std::placeholders::_1,std::placeholders::_2);

//     qiangsir::BaseClient::ptr client = qiangsir::ClientFactory::create("127.0.0.1", 9090);
//     client->setMessageCallBack(message_cb);
//     client->connect();

//     auto rpc_req = qiangsir::BaseMessageFactory::create<qiangsir::RpcRequest>();
//     rpc_req->setId("777777");
//     rpc_req->setType(qiangsir::MType::REQ_RPC);
//     rpc_req->setMethod("Add");
//     Json::Value param;
//     param["num1"] = 11;
//     param["num2"] = 22;
//     rpc_req->setParams(param);
//     client->send(rpc_req);

//     auto topic_req = qiangsir::BaseMessageFactory::create<qiangsir::TopicRequest>();
//     topic_req->setId("888888");
//     topic_req->setType(qiangsir::MType::REQ_TOPIC);
//     topic_req->setTopicKey("NEWS");
//     topic_req->setOptype(qiangsir::TopicOptype::TOPIC_PUBLISH);
//     topic_req->setMessage("大家好");
//     client->send(topic_req);

//     std::this_thread::sleep_for(std::chrono::seconds(5));

//     return 0;
// }

/* verson2-------test------*/

// void addResponse(const Json::Value& result)
// {
//     std::cout<<"result :"<<result.asInt()<<std::endl;

// }
// int main()
// {
//     qiangsir::Dispatcher::ptr dispatcher = std::make_shared<qiangsir::Dispatcher>();

//     auto requestor = std::make_shared<qiangsir::client::Requestor>();
//     auto rpc_caller = std::make_shared<qiangsir::client::RpcCaller>(requestor);

//     auto rpc_rsp = std::bind(&qiangsir::client::Requestor::onResponse, requestor.get(),
//                              std::placeholders::_1, std::placeholders::_2);

//     dispatcher->registerHandler<qiangsir::BaseMessage>(qiangsir::MType::RSP_RPC, rpc_rsp);

//     auto message_cb = std::bind(&qiangsir::Dispatcher::onMessage, dispatcher.get(),
//                                 std::placeholders::_1, std::placeholders::_2);

//     qiangsir::BaseClient::ptr client = qiangsir::ClientFactory::create("127.0.0.1", 9090);
//     client->setMessageCallBack(message_cb);
//     client->connect();
//     auto conn = client->connection();
//     Json::Value params, result;
//     params["num1"] = 11;
//     params["num2"] = 33;
//     bool ret = rpc_caller->call(conn, "Add", params, result);
//     if (ret != false)
//     {
//         std::cout << "result:" << result.asInt() << std::endl;
//     }
//     DLOG("同步rpc调用完毕");
//     std::future<Json::Value> res_future;
//     params["num1"] = 33;
//     params["num2"] = 44;
//     ret = rpc_caller->call(conn, "Add", params, res_future);
//     if (ret != false)
//     {
//         result = res_future.get();
//         std::cout << "result:" << result.asInt() << std::endl;
//     }

//     DLOG("异步rpc调用完毕");

//     params["num1"] = 44;
//     params["num2"] = 55;
//     ret = rpc_caller->call(conn, "Add", params, addResponse);

//     DLOG("回调rpc调用完毕-----------------");

//     std::this_thread::sleep_for(std::chrono::seconds(3));
//     client->shutDown();

//     return 0;
// }


/* verson3-------test------*/

#include "../../common/detail.hpp"
#include "../../client/client.hpp"
#include <chrono>
#include <iostream>
#include <thread>

void addResponse(const Json::Value& result)
{
    std::cout<<"result :"<<result.asInt()<<std::endl;

}
int main()
{


    qiangsir::client::RpcClient client(false,"127.0.0.1",8888);

    Json::Value params, result;
    params["num1"] = 11;
    params["num2"] = 33;
    bool ret = client.call("Add",params,result);
    if (ret != false)
    {
        std::cout << "result:" << result.asInt() << std::endl;
    }
    DLOG("同步rpc调用完毕");
    std::future<Json::Value> res_future;
    params["num1"] = 33;
    params["num2"] = 44;
    ret = client.call("Add",params,res_future);
    if (ret != false)
    {
        result = res_future.get();
        std::cout << "result:" << result.asInt() << std::endl;
    }

    DLOG("异步rpc调用完毕");

    params["num1"] = 44;
    params["num2"] = 55;
    ret = client.call("Add",params,addResponse);

    DLOG("回调rpc调用完毕-----------------");

    std::this_thread::sleep_for(std::chrono::seconds(3));

    return 0;
}
